
#include "CKiloutouClient.h"
#include <Ibquery.hpp>

//------------------------------------------------------------------------------
__fastcall CKiloutouClient::CKiloutouClient( void )
{
    MaRequete = NULL;
}
//------------------------------------------------------------------------------
__fastcall CKiloutouClient::CKiloutouClient( int nID_CLIENT ,const AnsiString &asNom, const AnsiString &asPrenom)
{
    _AffecterClient( nID_CLIENT, asNom, asPrenom);
}
//------------------------------------------------------------------------------
__fastcall CKiloutouClient::CKiloutouClient( int nID_CLIENT ,const AnsiString &asNom, const AnsiString &asPrenom, const int &nAge)
{
    _AffecterClient( nID_CLIENT, asNom, asPrenom, nAge);
}
//------------------------------------------------------------------------------
__fastcall CKiloutouClient::~CKiloutouClient()
{
    if(MaRequete) {
        delete MaRequete;
        MaRequete = NULL;
    }
}
//------------------------------------------------------------------------------

void __fastcall CKiloutouClient::_AffecterClient( int nID_CLIENT ,const AnsiString &asNom, const AnsiString &asPrenom, const int &nAge )
{
    m_nID_CLIENT    = nID_CLIENT;
    m_asNom         = asNom;
    m_asPrenom      = asPrenom;
    m_nAge          = nAge;
}

//------------------------------------------------------------------------------
AnsiString __fastcall CKiloutouClient::GetLibelle( void )
{
    if( Age == AGE_Inconnu )
        return Nom + " " + Prenom;
    else
        return Nom + " " + Prenom + " (" + Age + " ans)";
}
//------------------------------------------------------------------------------

void __fastcall CKiloutouClient::Open(const AnsiString &asQuery)
{
    try
    {
        if(MaRequete) {
            delete MaRequete;
            MaRequete = NULL;
        }

        MaRequete            = NULL;
        MaRequete            = new TIBQuery((TComponent*)0);
        MaRequete->Database  = dmKiloutou->dbKiloutou;
        MaRequete->SQL->Text = asQuery;
        MaRequete->Open();
    }catch( Exception &e ) {
        ShowMessage((AnsiString)"Erreur : "+e.Message);
    }
}
//------------------------------------------------------------------------------
void __fastcall CKiloutouClient::First( void )
{
    MaRequete->First();
}

//------------------------------------------------------------------------------
bool __fastcall CKiloutouClient::Next ( void )
{
    bool bRetour = false;

    MaRequete->Next();
    if(!MaRequete->Eof)
        bRetour = true;

    return bRetour;

}

//------------------------------------------------------------------------------
void __fastcall CKiloutouClient::Delete ( TDBGrid *lbClients )
{
    try
    {

        lbClients->DataSource->DataSet->Delete();
        /*if(MaRequete) {
            delete MaRequete;
            MaRequete = NULL;
        }
        MaRequete           = NULL;
        MaRequete           = new TIBQuery((TComponent*)0);
        MaRequete->Database = dmKiloutou->dbKiloutou;

        MaRequete->SQL->Text = asQuery;
        MaRequete->ExecSQL();
        MaRequete->SQL->Clear();

        MaRequete->SQL->Text = "Commit";
        MaRequete->ExecSQL();
        Close();*/
    }catch( Exception &e ) {
        ShowMessage((AnsiString)"Erreur : "+e.Message);
    }
}

//------------------------------------------------------------------------------
void __fastcall CKiloutouClient::Insert ( const AnsiString &asNom, const AnsiString &asPrenom, const int &nAge )
{
    try
    {
        dmKiloutou->tClient->Insert();
        asClient(NOM) = asNom;
        asClient(PRENOM) = asPrenom;
        nClient(AGE) = nAge;
        dmKiloutou->tClient->Post();
        dmKiloutou->trKiloutou->CommitRetaining();

        /*
        if(MaRequete) {
            delete MaRequete;
            MaRequete = NULL;
        }

        MaRequete           = NULL;
        MaRequete           = new TIBQuery((TComponent*)0);
        MaRequete->Database = dmKiloutou->dbKiloutou;

        //MaRequete->SQL->Text = (AnsiString)"INSERT INTO CLIENT (NOM, PRENOM, AGE) VALUES(\'"+asNom+"\',\'"+asPrenom+"\',"+nAge+")";
        ShowMessage( MaRequete->SQL->Text );
        MaRequete->ExecSQL();
        MaRequete->SQL->Clear();

        MaRequete->SQL->Text = "Commit";
        MaRequete->ExecSQL();
        Close();
        */
        
    }catch( Exception &e ) {
        ShowMessage((AnsiString)"Erreur : "+e.Message);
    }
}

//------------------------------------------------------------------------------
void __fastcall CKiloutouClient::Update ( const AnsiString &asNom, const AnsiString &asPrenom, const int &nAge)
{
    try
    {
        dmKiloutou->tClient->Edit();
        asClient(NOM) = asNom;
        asClient(PRENOM) = asPrenom;
        asClient(AGE) = nAge;
        dmKiloutou->tClient->Post();
        dmKiloutou->trKiloutou->CommitRetaining();
        /*
        if(MaRequete) {
            delete MaRequete;
            MaRequete = NULL;
        }
        MaRequete           = NULL;
        MaRequete           = new TIBQuery((TComponent*)0);
        MaRequete->Database = dmKiloutou->dbKiloutou;

        MaRequete->SQL->Text = asQuery;
        MaRequete->ExecSQL();
        MaRequete->SQL->Clear();

        MaRequete->SQL->Text = "Commit";
        MaRequete->ExecSQL();
        Close();
        */
    }catch( Exception &e ) {
        ShowMessage((AnsiString)"Erreur : "+e.Message);
    }
}

//------------------------------------------------------------------------------
void __fastcall CKiloutouClient::Close (void)
{
    MaRequete->SQL->Clear();

    if(MaRequete) {
        delete MaRequete;
        MaRequete = NULL;
    }
}

//------------------------------------------------------------------------------
void __fastcall CKiloutouClient::EffacerTable( void )
{
    dmKiloutou->dsClient->DataSet->Delete();
    dmKiloutou->trKiloutou->CommitRetaining();

        //Refresh du dataset
    dmKiloutou->dsetLocation->Active = false;
    dmKiloutou->dsetLocation->Active = true;
}
